package com.wanshi.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

@WebListener()
public class MyServletContextListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        Properties prop = new Properties();
        String path = this.getClass().getClassLoader().getResource("jingdong.properties").getPath();
        try {
            prop.load(new FileReader(path));
            String picUrl = prop.getProperty("pic_url");
            String picSavePath = prop.getProperty("pic_save_path");

            ServletContext context = sce.getServletContext();
            context.setAttribute("pic_url",picUrl);
            context.setAttribute("pic_save_path",picSavePath);

            System.out.println("监听到ServletContext域创建,存入picurl-------------");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
